package org.bite.lesson02;

public class Demo_208 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{
            while(!Thread.currentThread().isInterrupted()){
                System.out.println("hello Thread~");
                try{//线程大部分时间都在sleep
                    //sleep时被中断会触发异常
                    Thread.sleep(1000);
                }catch(InterruptedException e){
                    //要处理中断逻辑
                    e.printStackTrace();
                    System.out.println("休眠被中断啦！");
                    break;
                }
            }
            System.out.println("线程退出……");
        });
        System.out.println("线程是否存活：" + thread.isAlive());
        // 启动线程
        thread.start();
        // 休眠一会
        Thread.sleep(1000);
        System.out.println("线程是否存活：" + thread.isAlive());
        // 中断线程，发出中断信号
        thread.interrupt();
        // 等待线程销毁
        Thread.sleep(100);
        // 查看是否存活
        System.out.println("线程是否存活：" + thread.isAlive());
    }
}
